<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="老师名称" prop="teacherName">
        <el-input
          v-model="queryParams.teacherId"
          placeholder="请输入老师名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseId"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课表时间" prop="signDate">
        <el-date-picker clearable
          v-model="queryParams.signDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择课表时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:table:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:table:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:table:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:table:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="老师ID" align="center" prop="teacherId"  v-if="false"/>
      <el-table-column label="老师名称" align="center" prop="teacherName" />
      <el-table-column label="课程ID" align="center" prop="courseId" v-if="false"/>
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="课表时间" align="center" prop="signDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程开始时间" align="center" prop="courseStartTime" />
      <el-table-column label="课程结束时间" align="center" prop="courseEndTime" />
      <el-table-column label="课程介绍" align="center" prop="courseBrief" />
      <el-table-column label="标题颜色" align="center" prop="titleColor" />
      <el-table-column label="主体颜色" align="center" prop="subjectColor" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:table:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:table:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="老师" prop="teacherId">
          <el-select v-model="form.teacherId"  style="width: 380px;" placeholder="请选择老师" :popper-append-to-body="false">
            <el-option
              v-for="item in teacherList"
              :key="item.id"
              :label="item.teacherName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程" prop="courseId">
          <el-select v-model="form.courseId" style="width: 380px;" placeholder="请选择课程" :popper-append-to-body="false">
            <el-option
              v-for="item in courseList"
              :key="item.id"
              :label="item.courseName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课表时间" prop="signDate">
          <el-date-picker clearable
            v-model="form.signDate" style="width: 380px;"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择课表时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="课表开始时间" prop="courseStartTime">
          <el-select v-model="form.courseStartTime"  style="width: 380px;" @change="startChanged" placeholder="请选择课表开始时间" :popper-append-to-body="false">
            <el-option
              v-for="item in timeList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="课表结束时间" prop="courseEndTime">
          <el-select v-model="form.courseEndTime"  style="width: 380px;" @change="endChanged" placeholder="请选择课表结束时间" :popper-append-to-body="false">
            <el-option
              v-for="item in timeList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程介绍" prop="courseBrief">
          <el-input  type="textarea" v-model="form.courseBrief" placeholder="请输入课程介绍" autosize/>
        </el-form-item>
        <el-form-item label="标题颜色" prop="titleColor">
          <el-color-picker v-model="form.titleColor"></el-color-picker>
          <!-- <el-input  type="text" v-model="form.titleColor" placeholder="请输入标题颜色" autosize/> -->
        </el-form-item>
        <el-form-item label="主题颜色" prop="subjectColor">
          <el-color-picker v-model="form.subjectColor"></el-color-picker>
          <!-- <el-input  type="text" v-model="form.subjectColor" placeholder="请输入主题颜色" autosize/> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTable, getTable, delTable, addTable, updateTable,courseList,teacherList } from "@/api/system/table";

export default {
  name: "Table",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课表格数据
      tableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherId: null,
        courseId: null,
        signDate: null,
        courseStartTime: null,
        courseEndTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        courseId : [
          { required: true, message: '请选择课程信息', trigger: 'blur' }
        ],teacherId  : [
          { required: true, message: '请选择老师信息', trigger: 'blur' }
        ],
        signDate  : [
          { required: true, message: '请选择课表时间', trigger: 'blur' }
        ],
        courseStartTime : [
          { required: true, message: '课表开始时间', trigger: 'blur' }
        ],
        courseEndTime : [
          { required: true, message: '课表结束时间', trigger: 'blur' }
        ]
      },
      //课程名称
      courseList:[],
      //课程名称
      teacherList:[],
      timeList:[{id: 8,name: '08:00'},{id: 9,name: '09:00'},{id: 10,name: '10:00'},
        {id: 11,name: '11:00'},{id: 12,name: '12:00'},{id: 13,name: '13:00'},{id: 14,name: '14:00'},{id: 15,name: '15:00'},
        {id: 16,name: '16:00'},{id: 17,name: '17:00'},{id: 18,name: '18:00'},{id: 19,name: '19:00'},{id: 20,name: '20:00'}]
    };
  },
  created() {
    this.getList();
    this.getCourseList();
    this.getTeacherList();
  },
  methods: {
    //开始时间
    startChanged(value){
      var startTime = this.timeList.find((item) => {return value ===  item.id})
      var endTime = this.timeList.find((item) => {return this.form.courseEndTime ===  item.id})
      if(null != startTime && null != endTime){
        if(startTime.name >= endTime.name){
          this.$modal.msgError("选择的课程开始时间必须小于课程结束时间");
        }
      }
    },
    //结束时间
    endChanged(value){
      var startTime = this.timeList.find((item) => {return this.form.courseStartTime ===  item.id})
      var endTime = this.timeList.find((item) => {return  value ===  item.id})
      if(startTime.name >= endTime.name){
        this.$modal.msgError("选择的课程结束时间必须大于课程开始时间");
      }
    },
    /** 查询课列表 */
    getList() {
      this.loading = true;
      listTable(this.queryParams).then(response => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getCourseList(){
      courseList().then(res => {
        console.log(res)
        if(res.code == 200){
          this.courseList = res.data
        }else{
          console.log("查询不到数据")
        }
      }).catch(function (error) {
        console.log(error)
      })
    },
    getTeacherList(){
      teacherList().then(res => {
        console.log(res)
        if(res.code == 200){
          this.teacherList = res.data
        }else{
          console.log("查询不到数据")
        }
      }).catch(function (error) {
        console.log(error)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teacherId: null,
        teacherName: null,
        courseId: null,
        courseName: null,
        courseBrief: null,
        signDate: null,
        courseStartTime: null,
        courseEndTime: null,
        courseIconUrl: null,
        courseIconName: null,
        titleColor:null,
        subjectColor:null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        startId:null,
        endId:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTable(id).then(response => {
        this.form = response.data;
        this.$set(this.form, "courseStartTime", response.startId);
        this.$set(this.form, "courseEndTime", response.endId);
        this.open = true;
        this.title = "修改课";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            var course = this.courseList.find((item) => {return this.form.courseId ===  item.id})
            var teacher = this.teacherList.find((item) => {return this.form.teacherId ===  item.id})
            this.form.courseName = course.courseName;
            this.form.teacherName = teacher.teacherName;

            var startTime = this.timeList.find((item) => {return this.form.courseStartTime ===  item.name})
            var endTime = this.timeList.find((item) => {return this.form.courseEndTime === item.name})
            if(null == startTime || undefined == startTime){
              startTime = this.timeList.find((item) => {return this.form.courseStartTime ===  item.id})
            }
            if(null == endTime || undefined == endTime){
              startTime = this.timeList.find((item) => {return this.form.courseEndTime === item.id})
            }

            if(startTime.name >= endTime.name){
              this.$modal.msgError("选择的课程开始时间必须小于课程结束时间");
              return;
            }
            this.form.courseStartTime = startTime.name;
            this.form.courseEndTime = endTime.name;
            updateTable(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            var course = this.courseList.find((item) => {return this.form.courseId ===  item.id})
            var teacher = this.teacherList.find((item) => {return this.form.teacherId ===  item.id})
            this.form.courseName = course.courseName;
            this.form.teacherName = teacher.teacherName;

            var startTime = this.timeList.find((item) => {return this.form.courseStartTime ===  item.id})
            var endTime = this.timeList.find((item) => {return this.form.courseEndTime === item.id})

            if(startTime.name >= endTime.name){
              this.$modal.msgError("选择的课程开始时间必须小于课程结束时间");
              return;
            }
            this.form.courseStartTime = startTime.name;
            this.form.courseEndTime = endTime.name;
            addTable(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除课编号为"' + ids + '"的数据项？').then(function() {
        return delTable(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/table/export', {
        ...this.queryParams
      }, `table_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
